<template>
  <div class="dashboard">
    <el-row :gutter="20" class="stats-row">
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon category">
              <el-icon><Menu /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ stats.categoryCount }}</div>
              <div class="stat-label">分类总数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon food">
              <el-icon><Food /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ stats.foodCount }}</div>
              <div class="stat-label">菜品总数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon choice">
              <el-icon><Odometer /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ stats.todayChoices }}</div>
              <div class="stat-label">今日选择</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon popular">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ stats.popularCount }}</div>
              <div class="stat-label">热门菜品</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="content-row">
      <el-col :span="12">
        <el-card>
          <template #header>
            <span>热门菜品 TOP 10</span>
          </template>
          <el-table :data="popularFoods" size="small">
            <el-table-column prop="name" label="菜品名称" />
            <el-table-column prop="choice_count" label="选择次数" width="100" />
          </el-table>
        </el-card>
      </el-col>
      
      <el-col :span="12">
        <el-card>
          <template #header>
            <span>快捷操作</span>
          </template>
          <div class="quick-actions">
            <el-button type="primary" @click="$router.push('/categories')">
              <el-icon><Plus /></el-icon>
              添加分类
            </el-button>
            <el-button type="success" @click="$router.push('/foods')">
              <el-icon><Plus /></el-icon>
              添加菜品
            </el-button>
            <el-button type="info" @click="testRandom">
              <el-icon><Refresh /></el-icon>
              测试随机
            </el-button>
          </div>
          
          <div v-if="randomFood" class="random-result">
            <h4>随机结果：</h4>
            <p><strong>{{ randomFood.name }}</strong></p>
            <p class="description">{{ randomFood.description }}</p>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { getEnabledCategories } from '../api/category'
import { getAllEnabledFoods, getRandomFood } from '../api/food'
import { getPopularFoods, getTodayChoices } from '../api/statistics'
import { ElMessage } from 'element-plus'

export default {
  name: 'Dashboard',
  setup() {
    const stats = ref({
      categoryCount: 0,
      foodCount: 0,
      todayChoices: 0,
      popularCount: 0
    })
    const popularFoods = ref([])
    const randomFood = ref(null)
    
    const loadStats = async () => {
      try {
        // 获取分类统计
        const categoriesRes = await getEnabledCategories()
        stats.value.categoryCount = categoriesRes.data.length
        
        // 获取菜品统计
        const foodsRes = await getAllEnabledFoods()
        stats.value.foodCount = foodsRes.data.length
        
        // 获取今日选择统计
        const todayRes = await getTodayChoices()
        stats.value.todayChoices = todayRes.data.length
        
        // 获取热门菜品
        const popularRes = await getPopularFoods(7)
        popularFoods.value = popularRes.data
        stats.value.popularCount = popularRes.data.length
      } catch (error) {
        console.error('加载统计数据失败:', error)
      }
    }
    
    const testRandom = async () => {
      try {
        const res = await getRandomFood()
        randomFood.value = res.data
        ElMessage.success('随机选择成功！')
      } catch (error) {
        ElMessage.error('随机选择失败')
      }
    }
    
    onMounted(() => {
      loadStats()
    })
    
    return {
      stats,
      popularFoods,
      randomFood,
      testRandom
    }
  }
}
</script>

<style scoped>
.dashboard {
  padding: 0;
}

.stats-row {
  margin-bottom: 20px;
}

.stat-card {
  height: 100px;
}

.stat-content {
  display: flex;
  align-items: center;
  height: 100%;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 24px;
  color: white;
}

.stat-icon.category {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.food {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.choice {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.popular {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-top: 5px;
}

.content-row {
  margin-top: 20px;
}

.quick-actions {
  margin-bottom: 20px;
}

.quick-actions .el-button {
  margin-right: 10px;
  margin-bottom: 10px;
}

.random-result {
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.random-result h4 {
  margin-bottom: 8px;
  color: #303133;
}

.random-result p {
  margin: 5px 0;
}

.description {
  color: #606266;
  font-size: 14px;
}
</style>